package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.service.order.WeixinPayService;
import com.qingcheng.service.seckill.SeckillOrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:boy
 * @Date:2019/7/24
 * @description:
 * @ModifiedBy:
 */
@RestController
@RequestMapping("/pay")
public class PayController {
    @Reference
    private WeixinPayService weixinPayService;
    @Reference
    private SeckillOrderService seckillOrderService;

    @GetMapping("/createNative")
    public Map createNative() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        if (username != null) {
            SeckillOrder seckillOrder = seckillOrderService.findByUsername(username);
            if (seckillOrder != null && username.equals(seckillOrder.getUserId())) {
                int money = seckillOrder.getMoney().intValue();
                return weixinPayService.createNative(seckillOrder.getId() + "", money, "http://qingcheng.cross.echosite.cn/pay/notify.do");
            }
        }
        return new HashMap();
    }

    @GetMapping("/queryPayStatus")
    public Map<String, String> queryPayStatus(String orderId) {
        Map<String, String> resultMap = weixinPayService.queryPayStatus(orderId);
        if (resultMap.get("return_code").equalsIgnoreCase("success")
                && resultMap.get("result_code").equalsIgnoreCase("success")) {
            String tradeState = resultMap.get("trade_state");
            if ("success".equalsIgnoreCase(tradeState)) {
                String username = SecurityContextHolder.getContext().getAuthentication().getName();
                seckillOrderService.updatePayStatus(resultMap.get("out_trade_no"), resultMap.get("transaction_id"), username);
            }
        }
        return resultMap;
    }
    @RequestMapping("/notify.do")
    public Map notifyLogic(HttpServletRequest request){
        System.out.println("开始回调。。。。。");
        //System.out.println("支付成功");
        InputStream inputStream;
        try {
            inputStream=request.getInputStream();
            ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len;
            while ((len=inputStream.read(buffer))>0){
                outputStream.write(buffer,0,len);
            }
            outputStream.close();
            inputStream.close();
            String result=new String(outputStream.toByteArray(),"utf-8");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new HashMap();
    }
}
